<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class Goods extends Model
{
    //获取商品单一数据
    public static function find($id)
    {
        $find = Db::name('goods')->where('id', $id)->where('del', '0')->find();
        return $find;
    }
    //根据条件查询五条数据
    public static function wherefive($where)
    {
        $find = Db::name('goods')->where($where)->where('del', '0')->limit(0, 5)->select();
        return $find;
    }
    //获取更多商品
    public static function move($page, $limit = 10)
    {
        $tol = ($page - 1) * $limit;
        $list = Db::name('goods')->alias('a')->leftjoin('goods_cat b', 'a.cat_id=b.id')->field('a.*,b.name')->limit($tol, $limit)->order('a.is_hot')->order('a.px')->where('a.del', '0')->where('a.is_show', '1')->select();
        return $list;
    }
    //商品添加
    public static function add($data)
    {
        $data['add_date'] = time();
        $res = Db::name('goods')->insert($data);
        return $res;
    }

    //商品列表
    public static function list($where = "", $limit, $page)
    {
        $count = Db::name('goods')->alias('a')->leftjoin('goods_cat b', 'a.cat_id=b.id')->field('a.*,b.name')->where($where)->where('a.del', '0')->order('a.px')->count();
        //计算出从那条开始查询
        $tol = ($page - 1) * $limit;
        // 查询出当前页数显示的数据
        $list = Db::name('goods')->alias('a')->leftjoin('goods_cat b', 'a.cat_id=b.id')->field('a.*,b.name')->where($where)->where('a.del', '0')->order('a.px')->limit($tol, $limit)->select();
        return  ['data' => $list, 'count' => $count];
    }

    //商品编辑
    public static function edit($data)
    {
        $data['rep_date'] = time();
        $res = Db::name('goods')->update($data);
        return $res;
    }

    //商品删除
    public static function del($id)
    {
        $res = Db::name('goods')->where('id', $id)->update(['del' => '1', 'rep_date' => time()]);
        return $res;
    }
}
